<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['repertory:kcmData:add']"
        >新增
        </el-button>
      </el-col>-->
      <!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['repertory:kcmData:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['repertory:kcmData:remove']"
        >删除
        </el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['repertory:kcmData:export']"
        >
          导出
        </el-button>
      </el-col>

      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :cell-style="iCellStyle"
      :data="kcmDataList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="55" align="center" />
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="kcmCustWldm"
      />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="kcmWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="300"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column
        label="仓库"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="kcmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="storageLists" :value="scope.row.kcmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="库位"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="kcmKwdm"
      />
      <el-table-column
        label="客户"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="cusName"
      />
      <el-table-column
        label="库存数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="kcmKcsl"
      />
      <el-table-column
        label="占用数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="kcmZysl"
      />
      <el-table-column
        label="可用数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="kcmKykc"
      />
      <el-table-column
        label="单据编号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="kcmDjbh"
      />
      <el-table-column
        label="更新人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="kcmGgry"
      />
      <el-table-column
        label="更新日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="kcmGgrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.kcmGgrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['repertory:kcmData:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['repertory:kcmData:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="550px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label-width="125px" label="仓库代码" prop="itmCkdm">
            <el-select
              v-model="queryParams.kcmCkdm"
              style="width: 300px"
              placeholder="请输入仓库代码"
              clearable
            >
              <el-option
                v-for="dict in storageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="totalOrder">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.totalOrder"
              placeholder="请输入物料代码或品名规格或客户编码"
              clearable
              style="width: 300px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改仓库数据对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料代码" prop="kcmWldm">
          <el-input v-model="form.kcmWldm" placeholder="请输入物料代码" />
        </el-form-item>
        <el-form-item label="库存数量" prop="kcmKcsl">
          <el-input v-model="form.kcmKcsl" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="占用数量" prop="kcmZysl">
          <el-input v-model="form.kcmZysl" placeholder="请输入占用数量" />
        </el-form-item>
        <el-form-item label="单据编号" prop="kcmDjbh">
          <el-input v-model="form.kcmDjbh" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="更新人员" prop="kcmGgry">
          <el-input v-model="form.kcmGgry" placeholder="请输入更新人员" />
        </el-form-item>
        <el-form-item label="更新日期" prop="kcmGgrq">
          <el-date-picker
            clearable
            size="small"
            v-model="form.kcmGgrq"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择更新日期"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listKcmData,
  getKcmData,
  delKcmData,
  addKcmData,
  updateKcmData,
  exportKcmData,
} from "@/api/repertory/kcmData";
import { getAll, getstorageType } from "@/api/repertory/Storage";

export default {
  name: "KcmData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 仓库数据表格数据
      kcmDataList: [],
      //仓库信息
      storageLists: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        totalOrder: null,
        kcmCkdm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        kcmWldm: [
          { required: true, message: "物料代码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    getstorageType(0).then((response) => {
      this.storageLists = response.data;
      for (let item of this.storageLists) {
        item.dictLabel = item.storageName;
        item.dictValue = "" + item.storageId;
        item.listClass = "default";
      }
    });
    this.getList();
  },
  methods: {
    /** 分页跳转 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 查询仓库数据列表 */
    getList() {
      this.loading = true;
      listKcmData(this.queryParams).then((response) => {
        this.kcmDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        kcmId: null,
        kcmWldm: null,
        kcmKcsl: null,
        kcmZysl: null,
        kcmDjbh: null,
        kcmGgry: null,
        kcmGgrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.kcmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const kcmId = row.kcmId || this.ids;
      getKcmData(kcmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库数据";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询仓库数据";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.kcmId != null) {
            updateKcmData(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKcmData(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const kcmIds = row.kcmId || this.ids;
      this.$confirm(
        '是否确认删除仓库数据编号为"' + kcmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delKcmData(kcmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有仓库数据数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportKcmData(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //占用数量小于0
      if (row.kcmZysl < "0") {
        return "padding:0px 0;color:#0323af";
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
